﻿var ToDoApp = {};
ToDoApp.items = [];

ToDoApp.addItem = function () {
	"use strict";
	var newItem = document.getElementById("item-input");
	var holder = {
		timestamp: Date.now(),
		taskName: newItem.value,
		isDone: false
	};
	if (newItem.value !== '') {
		ToDoApp.items.push(holder);
		newItem.value = "";
		ToDoApp.writeItems();
	} else(alert('Please Insert New Task!'))
};
ToDoApp.clearDone = function () {
	"use strict";
	var holderArray = [];
	for (var x in ToDoApp.items) {
		if (!ToDoApp.items[x].isDone) {
			holderArray.push(ToDoApp.items[x]);
		}
	}
	ToDoApp.items = holderArray;
	ToDoApp.writeItems();
};
ToDoApp.toggleDone = function (index) {
	"use strict";
	ToDoApp.items[index].isDone = !ToDoApp.items[index].isDone;
	//document.getElementById("list").innerHTML = (new Date(ToDoApp.items[index].timestamp)).toLocaleTimeString();
	ToDoApp.writeItems();/*When i comment this it clears*/
};
ToDoApp.writeItems = function () {
	"use strict";
	var stringHolder = "";
	for (var i in ToDoApp.items) {
		if (ToDoApp.items[i].isDone) {
			stringHolder += "	" + "<span class='done' onclick='ToDoApp.toggleDone(" + i + ")'>";			
		}else {
			stringHolder += "<span class='' onclick='ToDoApp.toggleDone(" + i + ")'>";
		}
		stringHolder += ToDoApp.items[i]['taskName'];
		stringHolder += "	" + (new Date(ToDoApp.items[i].timestamp)).toLocaleTimeString();
		stringHolder += "</span>";
		stringHolder += "<br/>";
	}
	document.getElementById("list").innerHTML = stringHolder;
};